package com.apes.framework.plugin.form.parser.element;

import com.apes.framework.plugin.form.parser.matcher.AttrsMatcher;
import com.apes.framework.plugin.form.parser.matcher.DomainMatcher;
import com.apes.framework.plugin.form.parser.matcher.ExpressionMatcher;
import com.apes.framework.plugin.form.parser.node.FormNodeParser;
import com.apes.framework.plugin.filereader.xml.ApesElement;
import com.apes.framework.plugin.filereader.xml.ApesElementParser;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 功能：notebook解析
 *
 * @author xul
 * @create 2018-04-16 16:12
 */
@Component
public class TabsElementParser implements ApesElementParser<ApesElement, Map> {

    /**
     * 可解析的元素列表
     **/
    private final String[] elementNames = {"tabs"};

    /**
     * 功能：可解析节点域
     */
    @Override
    public String getDomain() {
        return FormNodeParser.domain;
    }

    /**
     * 功能：可解析的元素列表
     */
    @Override
    public String[] getNames() {
        return elementNames;
    }

    @Override
    public Map parse(ApesElement element) {
        Map node = new HashMap();
        node.put("type", "tabs");
        node.put("tabs", element.getNestedElements().stream()
                .filter(e -> e.getName().equals("tab"))
                .map(apesElement -> apesElement.parse()).collect(Collectors.toList()));
        List l = new ArrayList();
        l.add(node);

        parseAttributes(node,element);

        element.getNestedElements().stream().filter((e) -> e.getName().equals("callback")).forEach(e -> {
             node.put("callback", e.parse());
        });

        Map result = new HashMap();
        result.put("renderData", l);
        return result;
    }

    protected void parseAttributes(Map node, ApesElement element) {
        element.getAttributes().forEach((k, v) -> {
            if (k.equals("title")) {
                node.put("title", v.trim());
            } else { //currentPanel, ...
                node.put(k.trim(), v.trim());
            }
        });
    }


}
